﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;

public partial class seal_seal_order : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
            return;
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        if (!IsPostBack)
        {
            try
            {
                TRN_JUCHU_TANTO_NAME.Text = loginModel.OfficeTanto;
                TRADER_REG_NO12.Text = StringUtil.isEmpty(loginModel.Parent.TraderRegNo)?"": loginModel.Parent.TraderRegNo.Substring(0, 2);
                TRADER_REG_NO22.Text = StringUtil.isEmpty(loginModel.Parent.TraderRegNo) ? "" : loginModel.Parent.TraderRegNo.Substring(2, 5);
                MST_TraderPersister dao = new MST_TraderPersister();
                MstTrader trader = dao.getDetail(loginModel.Parent.TraderRegNo);
                if (trader != null && trader.TraderSts.Equals(MstTrader.STATUS_NORMAL))
                {
                    TRADER_REG_NO_LABEL.Text = trader.TraderName;
                }
            }
            catch (Exception ex)
            {
                LogManager.error("seal_seal_order :" + ex.Message);
            }
        }


    }

    protected void login_confirm(object sender, EventArgs e)
    {
        try
        {
            Order order = new Order();
           
            //モデルに値をセットする。(TRN_Juchu_H)
            TrnJuchuh tjh = new TrnJuchuh();
            tjh.JuchuNo = "";
            tjh.UnionCode = "";
            tjh.BranchCode = 0;
            tjh.JuchuTantoName = TRN_JUCHU_TANTO_NAME.Text;
            tjh.HopeYear = TRN_HOPE_YEAR.Text;
            tjh.HopeMonth = TRN_HOPE_MONTH.Text;
            tjh.HopeDay = TRN_HOPE_DAY.Text;
            if (StringUtil.isValidDate(TRN_HOPE_YEAR.Text, TRN_HOPE_MONTH.Text, TRN_HOPE_DAY.Text))
            {
                
                CultureInfo culture = new CultureInfo("ja-JP");
                tjh.HopeDate = Convert.ToDateTime((int.Parse(tjh.HopeYear) + 2000) + "/" + int.Parse(tjh.HopeMonth) + "/" + int.Parse(tjh.HopeDay), culture);

            }
            else if (StringUtil.isEmpty(TRN_HOPE_YEAR.Text) && StringUtil.isEmpty(TRN_HOPE_MONTH.Text) && StringUtil.isEmpty(TRN_HOPE_DAY.Text))
            {
                tjh.HopeDate = null;
            }
            
            tjh.HopeTimeClss = TRN_HOPE_TIME_CLSS1.Checked ? TrnJuchuh.HOPE_TIME_CLSS_NONE : ( TRN_HOPE_TIME_CLSS2.Checked ? TrnJuchuh.HOPE_TIME_CLSS_MORNING : TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON );
            tjh.TotalSealQty = 0;
            //FAX OCRは使用しないので0固定
            tjh.OcrOrderQty = 0;

            TrnJuchud[] model = new TrnJuchud[5];
		    TrnJuchud tmpmodel = null;
            TrnJuchudId id = null;
            string sq = null;

		    //モデルに値をセットする。
			tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo="";
            id.JuchuRecNo=1;
            tmpmodel.Id=id;
            tmpmodel.PrintClss=TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1=TRADER_REG_NO11.Value;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO21.Value;
            tmpmodel.TraderRegNo = TRADER_REG_NO11.Value + TRADER_REG_NO21.Value;
            sq=TRN_SEAL_QTY1.Text;
            if (StringUtil.isEmpty(sq)) {
				    tmpmodel.SealQty = -1;
			} else if (sq.IndexOf("-") != -1) {
				    //マイナスが入力された場合には
				    tmpmodel.SealQty = -2;
			} else if (!StringUtil.isHankakuNumber(sq)) {
				    tmpmodel.SealQty = -2;
			} else {
                    tmpmodel.SealQty = int.Parse( TRN_SEAL_QTY1.Text );
			}
            if (tmpmodel.SealQty == -1) {
				    model[0] = null;
				    LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 1" );
			    } else {
                    LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 1" );
				    model[0] = tmpmodel;
		    }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 2;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO12.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO22.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO12.Text + TRADER_REG_NO22.Text;
            sq = TRN_SEAL_QTY2.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY2.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[1] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 2");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 2");
                model[1] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 3;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO13.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO23.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO13.Text + TRADER_REG_NO23.Text;
            sq = TRN_SEAL_QTY3.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY3.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[2] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 3");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 3");
                model[2] = tmpmodel;
            }


            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 4;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO14.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO24.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO14.Text + TRADER_REG_NO24.Text;
            sq = TRN_SEAL_QTY4.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY4.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[3] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 4");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 4");
                model[3] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = "";
            id.JuchuRecNo = 5;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO15.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO25.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO15.Text + TRADER_REG_NO25.Text;
            sq = TRN_SEAL_QTY5.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY5.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[4] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 5");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 5");
                model[4] = tmpmodel;
            }

            order.setJuchu_D(model);
            order.setJuchu_H(tjh);

            //検証
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            TRN_JuchuManager validation = new TRN_JuchuManager();
            List<string> errorCodeList = validation.checkInsertOrder(order, Resources.Resource.ResourceManager, loginModel);
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count == 0)
            {
                Session["order"] = order;
                Response.Redirect("seal_order_conf.aspx");
            }


        }
        catch (Exception ex)
        {
            LogManager.error("seal_seal_order "+ex.Message);

           
        }


       

    }
}
